package com.mashibing.servicemap.service.impl;

import com.mashibing.internalcommon.constant.AmapConfigConstants;
import com.mashibing.internalcommon.constant.CommonStatusEnum;
import com.mashibing.internalcommon.dto.DicDistrict;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.servicemap.mapper.DicDistrictMapper;
import com.mashibing.servicemap.remote.MapDistrictClient;
import com.mashibing.servicemap.service.DistrictService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @title: DistrictServiceImpl
 * @Author Z先森
 * @Date: 2022/9/27 10:18
 * @Version 1.0
 */
@Service
@Slf4j
public class DistrictServiceImpl implements DistrictService {
    @Autowired
    private MapDistrictClient mapDistrictClient;
    @Autowired
    private DicDistrictMapper dicDistrictMapper;
    @Override
    public ResponseResult initDicDistrict(String keywords) {
        //1.请求地图服务
        String result = mapDistrictClient.initDicDistrict(keywords);
        //2.解析结果
        //3.插入数据库
        return parseDistrictEntity(result);
    }

    private ResponseResult parseDistrictEntity(String result) {
        JSONObject object = JSONObject.fromObject(result);
        //获取返回结果状态值 0 失败 1 成功
        int status = object.getInt(AmapConfigConstants.STATUS);
        if (status != 1){
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(),CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
        }
        //获取国家行政区列表
        JSONArray countryDistrictsArray = object.getJSONArray(AmapConfigConstants.DISTRICTS);
        for (int country = 0; country < countryDistrictsArray.size(); country++) {
            //获取国家行政区信息 封装对象保存库中
            JSONObject countryJsonObject = countryDistrictsArray.getJSONObject(country);
            String countryAddressCode = countryJsonObject.getString(AmapConfigConstants.ADCODE);
            String countryName = countryJsonObject.getString(AmapConfigConstants.NAME);
            String countryLevel = countryJsonObject.getString(AmapConfigConstants.LEVEL);
            String countryParentAddressCode = "0";
            insertDicDistrict(countryAddressCode,countryName,countryLevel,countryParentAddressCode);
            //获取省级行政区列表
            JSONArray provinceDistrictsArray = countryJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
            for (int province = 0; province < provinceDistrictsArray.size(); province++) {
                //获取省级行政区信息 封装对象保存库中
                JSONObject provinceJsonObject = provinceDistrictsArray.getJSONObject(province);
                String provinceAddressCode = provinceJsonObject.getString(AmapConfigConstants.ADCODE);
                String provinceName = provinceJsonObject.getString(AmapConfigConstants.NAME);
                String provinceLevel = provinceJsonObject.getString(AmapConfigConstants.LEVEL);
                insertDicDistrict(provinceAddressCode,provinceName,provinceLevel,countryAddressCode);
                //获取市级行政区列表
                JSONArray cityDistrictsArray = provinceJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                for (int city = 0; city < cityDistrictsArray.size(); city++) {
                    //获取市级行政区信息 封装对象保存库中
                    JSONObject cityJsonObject = cityDistrictsArray.getJSONObject(city);
                    String cityAddressCode = cityJsonObject.getString(AmapConfigConstants.ADCODE);
                    String cityName = cityJsonObject.getString(AmapConfigConstants.NAME);
                    String cityLevel = cityJsonObject.getString(AmapConfigConstants.LEVEL);
                    insertDicDistrict(cityAddressCode,cityName,cityLevel,provinceAddressCode);
                    //获取县级行政区列表
                    JSONArray districtArray = cityJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                    for (int district = 0; district < districtArray.size(); district++) {
                        //获取省级行政区信息 封装对象保存库中
                        JSONObject districtJsonObject = districtArray.getJSONObject(district);
                        String districtAddressCode = districtJsonObject.getString(AmapConfigConstants.ADCODE);
                        String districtName = districtJsonObject.getString(AmapConfigConstants.NAME);
                        String districtLevel = districtJsonObject.getString(AmapConfigConstants.LEVEL);
                        if(districtLevel.equals(AmapConfigConstants.STREET)){
                            continue;
                        }
                        insertDicDistrict(districtAddressCode,districtName,districtLevel,cityAddressCode);
                    }
                }

            }

        }
        return ResponseResult.success("");
    }




    public void insertDicDistrict(String addressCode, String addressName,String level ,String parentAddressCode){
        // 数据库对象
        DicDistrict district = new DicDistrict();
        district.setAddressCode(addressCode);
        district.setAddressName(addressName);
        int levelInt = generateLevel(level);
        district.setLevel(levelInt);

        district.setParentAddressCode(parentAddressCode);

        // 插入数据库
        dicDistrictMapper.insert(district);
    }

    public int generateLevel(String level){
        int levelInt = 0;
        if (level.trim().equals("country")){
            levelInt = 0;
        }else if(level.trim().equals("province")){
            levelInt = 1;
        }else if(level.trim().equals("city")){
            levelInt = 2;
        }else if(level.trim().equals("district")){
            levelInt = 3;
        }
        return levelInt;
    }
}
